package com.cskaoyan.controller;

import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketIssue;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.service.AdminIssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: SSMItem
 * @description 该Controller实现通用问题相关功能
 * @author: YY
 * @create: 2022-10-11 18:40
 **/

@RestController
@RequestMapping("admin/issue")
public class AdminIssueController {

    @Autowired
    AdminIssueService adminIssueService;


    /**
     * 请求url：http://localhost：8083/admin/issue/list?page=1&limit=20&sort=add_time&order=desc
     * <p>
     * 该方法为查看所有通用问题
     *
     * @return
     * @author: YY
     */
    @RequiresPermissions("admin:issue:list")
    @GetMapping("list")
    public BaseRespVo allIssue(BaseParam baseParam, String question) {



        List<MarketIssue> list = adminIssueService.selectIssue(baseParam, question);

        PageInfo<MarketIssue> pageInfo = new PageInfo<>(list);

        CommonData data = CommonData.data(pageInfo);

        return BaseRespVo.ok(data);

    }


    /**
     * 添加通用问题
     * 请求url：http://localhost：8083/admin/issue/create
     *
     * @param marketIssue 创建通用问题传入的信息
     * @return
     */
    @RequiresPermissions("admin:issue:create")
    @PostMapping("create")
    public BaseRespVo createIssue(@RequestBody MarketIssue marketIssue) {
        //先判断添加的通用问题是否重复
        String question = adminIssueService.selectIssueByQuestion(marketIssue.getQuestion());

        //没有逻辑删除
        if (question != null) {
            return BaseRespVo.issue("该通用问题已存在，请搜索后前往编辑！");
        }
        //存在但是逻辑删除


        MarketIssue Issue = adminIssueService.insertIssue(marketIssue);

        return BaseRespVo.ok(Issue);

    }


    /**
     * 修改通用问题
     * 请求url：http://localhost：8083/admin/issue/update
     *
     * @param marketIssue 修改后的信息
     * @return
     */
    @RequiresPermissions("admin:issue:update")
    @PostMapping("update")
    public BaseRespVo updateIssue(@RequestBody MarketIssue marketIssue) {

        MarketIssue Issue = adminIssueService.updateIssue(marketIssue);

        return BaseRespVo.ok(Issue);

    }


    /**
     * 删除通用问题
     * 请求url：http://localhost：8083/admin/issue/delete
     *
     * @param marketIssue
     * @return
     */
    @RequiresPermissions("admin:issue:delete")
    @PostMapping("delete")
    public BaseRespVo deleteIssue(@RequestBody MarketIssue marketIssue) {

        adminIssueService.deleteIssue(marketIssue);

        return BaseRespVo.ok(null);

    }
}
